package com.dayatang.android.Adapter;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.dayatang.android.Activity.R;
import com.dayatang.android.Activity.WorkstationOfPMOActivity;
import com.dayatang.android.Model.MyTask;
import com.dayatang.android.Task.BaseTask;

public class MyTaskListViewAdapter extends BaseAdapter {
	
	private Context context;
	private List<HashMap<String, Object>> listItem;
	private LayoutInflater listContainer;
	
	private String serverAddress;

	class ListItemView {
		private Button viewBtn;
		private TextView taskNumTextView;
		private TextView taskTextView;
		private TextView specIdTextView;
	}
	
	public MyTaskListViewAdapter(Context context,List<HashMap<String, Object>> listItem,String serverAddress) {
		this.context = context;
		this.listItem = listItem;
		this.serverAddress = serverAddress;
		listContainer = LayoutInflater.from(context);   
	}

	public int getCount() {
		return listItem.size();
	}

	public Object getItem(int position) {
		return listItem.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ListItemView  listItemView = null;
		
		if (convertView == null) {
			listItemView = new ListItemView();
			convertView = listContainer.inflate(R.layout.mytask_listview_item, null);
			
			listItemView.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
			listItemView.taskNumTextView = (TextView)convertView.findViewById(R.id.mytask_listview_item_num);
			listItemView.taskTextView = (TextView)convertView.findViewById(R.id.mytask_listview_item_task);
			listItemView.specIdTextView = (TextView)convertView.findViewById(R.id.mytask_listview_item_specid);
			
            convertView.setTag(listItemView);    

		} else {    
            listItemView = (ListItemView)convertView.getTag();    
        }  
		
		final MyTask myTask = new MyTask();
		myTask.setProjName((String)listItem.get(position).get("ProjName"));
		myTask.setSubProjectName((String)listItem.get(position).get("SubProjectName"));
		myTask.setResponsibleName((String)listItem.get(position).get("ResponsibleName"));
		myTask.setWorkStepName((String)listItem.get(position).get("WorkStepName"));
		myTask.setProgressStatus((String)listItem.get(position).get("ProgressStatus"));
		myTask.setSubProjectId((String)listItem.get(position).get("SubProjectId"));
		final String subprojectid = myTask.getSubProjectId();
		
		listItemView.taskNumTextView.setText((String)listItem.get(position).get("SubProjectName"));
		listItemView.taskTextView.setText((String)listItem.get(position).get("WorkStepName"));

		listItemView.specIdTextView.setText((String)listItem.get(position).get("SubProjectId"));
		
		listItemView.viewBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				alertDialog(subprojectid,myTask);
			}
		});
		
		return convertView;
	}
	
	private void alertDialog(final String subprojectid, final MyTask task) {
		
		LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		View view = mInflater.inflate(R.layout.task_details, null);
		
		((TextView)view.findViewById(R.id.task_project_name_editText)).setText(task.getProjName());
		((TextView)view.findViewById(R.id.task_responsible_person_editText)).setText(task.getResponsibleName());
		((TextView)view.findViewById(R.id.task_editText)).setText(task.getWorkStepName());
		((TextView)view.findViewById(R.id.task_status_editText)).setText(task.getSubProjectName());
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(context);
		dialog.setTitle(context.getString(R.string.task_details));
		dialog.setView(view);
		
		dialog.setPositiveButton(context.getString(R.string.task_finish), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				SubmitMyTask(subprojectid);
			}
		});
		
		dialog.setNegativeButton(context.getString(R.string.abolish), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		dialog.show();
	}
	
	private void SubmitMyTask(String subProjectid) {
		String url = serverAddress + "/remote/submit-task.action?subProject.id="+subProjectid;
		try {
			
			String result = new BaseTask(context).execute(url).get();
			
			if (result.equals("ok")) {
				context.startActivity(new Intent(context,WorkstationOfPMOActivity.class));
			}
			
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
	
}
